﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace sdamarketing.Administration
{
    public partial class AddPrivileges : osnova1
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Session["iduser"] == null || Session["username"] == null)
                    {
                        Response.Redirect("~/Default.aspx");
                    }
                    else
                    {
                        if (RolesAndPrivileges.AuthenticateUserWithPanel("Додај/Измени привилегија", Int32.Parse(Session["idRole"].ToString())) == 0)
                        {
                            Response.Redirect("~/Home.aspx");
                        }
                    }

                    DataSet panels = ReturnDataSet.ReturSet("SELECT idPanel, name FROM Panels");
                    panelsDropDown.DataValueField = "idPanel";
                    panelsDropDown.DataTextField = "name";
                    panelsDropDown.DataSource = panels;
                    panelsDropDown.DataBind();
                    panelsDropDown.Items.Insert(0, new ListItem("", "0"));
                    panelsDropDown.ClearSelection();
                }
                catch (Exception exception)
                {
                    SendToLogFile.SendRecordToFIle(DateTime.Now.ToString() + " -exc- " + exception.Message.ToString() + " -stackTrace- " + exception.StackTrace.ToString() + " -addPriviliges- ");
                }
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                RolesAndPrivileges.addRole(txbPrivilege.Text);
                lblMessage.Text = "Привилегијата е успешно додадена";
                txbPrivilege.Text = "";
            }
            catch (SqlException exception)
            {
                lblMessage.Text = "Привилегијата не е успешно додадена";
                SendToLogFile.SendRecordToFIle(DateTime.Now.ToString() + " -exc- " + exception.Message.ToString() + " -stackTrace- " + exception.StackTrace.ToString() + " -addPriviliges- ");
            }
        }

        protected void panelsDropDown_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                DataSet privileges = ReturnDataSet.ReturSet("SELECT idRole, name FROM Roles");
                priviligesCheckBoxes.DataValueField = "idrole";
                priviligesCheckBoxes.DataTextField = "name";
                priviligesCheckBoxes.DataSource = privileges;
                priviligesCheckBoxes.DataBind();

                DataSet havePriviliges = ReturnDataSet.ReturSet("SELECT idRole FROM tbr_PanelRoles WHERE idPanel =" + panelsDropDown.SelectedValue.ToString());

                for (int i = 0; i < havePriviliges.Tables["DataSet"].Rows.Count; i++)
                {
                    priviligesCheckBoxes.Items.FindByValue(havePriviliges.Tables["DataSet"].Rows[i].ItemArray[0].ToString()).Selected = true;
                }
            }
            catch (Exception exception)
            {
                SendToLogFile.SendRecordToFIle(DateTime.Now.ToString() + " -exc- " + exception.Message.ToString() + " -stackTrace- " + exception.StackTrace.ToString() + " -addPriviliges- ");
            }
        }

        protected void SaveChanges_Click(object sender, EventArgs e)
        {
            try
            {
                if (panelsDropDown.SelectedValue.ToString() == "0")
                {
                    lblMessageCheckBoxes.Text = "Ве молиме изберете панел";
                }
                else
                {
                    RolesAndPrivileges.DeleteRowsFromtPanelRoles(Int32.Parse(panelsDropDown.SelectedValue.ToString()));

                    foreach (ListItem cBox in priviligesCheckBoxes.Items)
                    {
                        if (cBox.Selected)
                        {
                            RolesAndPrivileges.addRoleForPanel(Int32.Parse(cBox.Value.ToString()), Int32.Parse(panelsDropDown.SelectedValue.ToString()));
                        }
                    }
                    lblMessageCheckBoxes.Text = "Привилегиите се испешно додадени";
                }
            }
            catch (Exception exception)
            {
                SendToLogFile.SendRecordToFIle(DateTime.Now.ToString() + " -exc- " + exception.Message.ToString() + " -stackTrace- " + exception.StackTrace.ToString() + " -addPriviliges- ");
            }
        }
    }
}
